﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TeacherScheduleaspx : System.Web.UI.Page
    {
        private static string       gTeacherEmail;
        public static string GetTeacherEmail()
        {
            return gTeacherEmail;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            gTeacherEmail = "dksxogudsla@naver.com";//Request.QueryString["teacherName"];            
            Init();
        }

        public void Init()
        {
            SetTable();

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = "data source=ANDREW-PC\\MYDB;" + "initial catalog=mydb;"
                + "user id=sa;" + "password=rntqkdl1";
            try
            {
                Conn.Open();
                Response.Write("db conn");
                string query = "select subject_schedule from TEACHER where teacher_email = '" + gTeacherEmail + "'";
                SqlCommand myCom = new SqlCommand(query, Conn);

                SqlDataReader myReader = myCom.ExecuteReader();

                while (myReader.Read())
                {
                    string schedule = myReader.GetString(0);
                    char weekday = schedule.ElementAt(0);
                    char time = schedule.ElementAt(1);
                    SetSchedule(weekday, time);             
                }

                myReader.Close();

                Conn.Close();
                Response.Write("db close");
            }
            catch
            {
                Response.Write("error");
            }
        }

        public void SetTable()
        {
            TableRow tr;
            TableCell td;

            tr = new TableRow();

            td = new TableCell();
            tr.Cells.Add(td);

            for (int i = 1; i < 8; i++)
            {
                td = new TableCell();
                td.Text = SetWeekday(i);
                tr.Cells.Add(td);
            }

            tr.BackColor = Color.FromName("#ccccff");
            Table1.Rows.Add(tr);

            for (int i = 1; i < 9; i++)
            {
                tr = new TableRow();

                // 제일 첫 셀에 이미지를 로드한다.
                td = new TableCell();
                //AspNet.Image img = new AspNet.Image();
                //img.ImageUrl = "images/asp.gif";
                //td.Controls.Add(img);
                td.Text = "" + i;
                tr.Cells.Add(td);

                // 루프를 돌면서 각 셀을 설정한다.
                for (int j = 1; j < 8; j++)
                {
                    td = new TableCell();
                    td.Text = "   ";//"td" + j;
                    tr.Cells.Add(td);
                }

                tr.BackColor = Color.White;
                Table1.Rows.Add(tr);

            }
        }

        private string SetWeekday(int which)
        {
            switch(which) {
                case 1:
                    return "월";
                case 2:
                    return "화";
                case 3:
                    return "수";
                case 4:
                    return "목";
                case 5:
                    return "금";
                case 6:
                    return "토";
                case 7:
                    return "일";
            }
            return " ";
        }

        public void SetSchedule(char weekday, char time)
        {
            int weekdayIndex = GetWeekdayIndex(weekday);
            int timeIndex = time - 48;

            TableRow tr = Table1.Rows[timeIndex];
            tr.Cells[weekdayIndex].Text = gTeacherEmail;
        }

        public int GetWeekdayIndex(char weekday)
        {
            switch (weekday)
            {
                case '월':
                    return 1;
                case '화':
                    return 2;
                case '수':
                    return 3;
                case '목':
                    return 4;
                case '금':
                    return 5;
                case '토':
                    return 6;
                case '일':
                    return 7;
            }
            return -1;
        }
    }
}